package com.aaa.jur.controller;


import com.aaa.common.entity.*;
import com.aaa.jur.service.RoleService;
import com.aaa.jur.service.UserRoleService;
import com.aaa.jur.service.UserService;
import com.aaa.jur.shiro.ShiroUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author AAATeacherChen
 * @since 2020-06-14
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService service;
    @Autowired
    RoleService roleService;
    @Autowired
    UserRoleService userRoleService;

    @ResponseBody
    @RequestMapping("/selectUser")
    public LayUiTable selectUser(Integer page, Integer limit) {
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("del_flag", 0);

        List<Map> users = service.selectUser(page, limit);
        int i = service.selectCount(wrapper);
        LayUiTable layUiTable = new LayUiTable();
        layUiTable.setCode(0);
        layUiTable.setMsg("成功");
        layUiTable.setCount(i);
        layUiTable.setData(users);
        return layUiTable;
    }

    @ResponseBody
    @RequestMapping("/selectRole")
    public List selectRole() {
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("del_flag", 0);
        List<Role> list = roleService.selectList(wrapper);
        return list;
    }
    @RequestMapping("/userList")
    public String userList(Model model) {
        /*Wrapper wrapper = new EntityWrapper();
        wrapper.eq("del_flag", 0);
        List<Role> list = roleService.selectList(wrapper);
        model.addAttribute("lists", list);*/
        return "userList";
    }

    @ResponseBody
    @RequestMapping("/saveUesr")
    public ResultData saveUesr(@RequestBody Map map) {
        //System.out.println(map);
        Map mapuser = (Map) map.get("list");
        User user = new User();
        //获取从页面传入的角色id,因为salt是在后台生成，所有由salt属性传入角色id的集合
        List list = (List) map.get("array");
        //生成随机码作为salt
        String salt = UUID.randomUUID().toString();
        //获取明文密码
        String message = (String) mapuser.get("password");
        //获取密文密码
        String encryption = ShiroUtil.encryptionBySalt(salt, message);
        //设置user的密码属性
        user.setPassword(encryption);
        //设置user对象的盐值属性
        user.setSalt(salt);
        String deptId = mapuser.get("deptId").toString();
        user.setDeptId(deptId);
        user.setUserName((String) mapuser.get("userName"));
        user.setLoginName((String) mapuser.get("loginName"));
        user.setEmail((String) mapuser.get("email"));
        user.setPhonenumber((String) mapuser.get("phonenumber"));
        user.setSex(mapuser.get("sex").toString());
        System.out.println(mapuser.get("sex"));
        System.out.println(user);
        //添加此用户关联的新角色
        boolean insert;
        UserRole userRole = new UserRole();
        if (mapuser.get("userId")!=null&&!mapuser.get("userId").equals("")){
            user.setUserId(mapuser.get("userId").toString());
            insert = service.updateById(user);
            Wrapper wrapper = new EntityWrapper();
            wrapper.eq("user_id", mapuser.get("userId"));
            userRoleService.delete(wrapper);
            userRole.setUserId(mapuser.get("userId").toString());
        }else {
            insert = service.insert(user);
            userRole.setUserId(user.getUserId());
        }
        for (Object lis : list) {
            if (lis != null && lis != "") {
                userRole.setRoleId((String) lis);
                 insert = userRoleService.insert(userRole);
            }
        }
        ResultData resultData;
        if (insert) {
            resultData = new ResultData(true, "成功", null);
        } else {
            resultData = new ResultData(false, "失败", null);
        }
        return resultData;
    }
    @ResponseBody
    @RequestMapping("/selectByid")
    public Map selectByid(String userId){
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("del_flag", 0);
        wrapper.eq("user_id", userId);
        //System.out.println(service.selectList(wrapper));
        User user = service.selectOne(wrapper);
        Map map=new HashMap();
        map.put("user",user);
        Wrapper wrapper1 = new EntityWrapper();
        wrapper1.eq("user_id", userId);
        List list = userRoleService.selectList(wrapper1);
        map.put("list",list);
        return map;
    }
    @ResponseBody
    @RequestMapping("/deleteUser")
    public ResultData deleteUser(User user){
        System.out.println(user);
        user.setDelFlag("1");
        boolean insert = service.updateById(user);
        ResultData resultData;
        if (insert) {
            resultData = new ResultData(true, "成功", null);
        } else {
            resultData = new ResultData(false, "失败", null);
        }
        return resultData;
    }

}

